package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class TradeViewImpl extends Composite implements TradeView {

	private static TradeViewImplUiBinder uiBinder = GWT
			.create(TradeViewImplUiBinder.class);

	interface TradeViewImplUiBinder extends UiBinder<Widget, TradeViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel profitPanel;
	@UiField
	SpanElement localProfitSpan;
	@UiField
	SpanElement overseeProfitSpan;
	@UiField
	SpanElement treasurySpan;

	@UiField
	HTMLPanel expensesPanel;
	@UiField
	SpanElement auxiliaSpan;
	@UiField
	SpanElement auxiliaExpensesSpan;
	@UiField
	SpanElement armySpan;
	@UiField
	SpanElement armyExpensesSpan;
	@UiField
	SpanElement marinesSpan;
	@UiField
	SpanElement navyExpensesSpan;

	@UiField
	HTMLPanel balancePanel;
	@UiField
	SpanElement balanceSpan;

	@UiField
	Button continueButton;

	public TradeViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;
	}

	@Override
	public void hideAll() {
		profitPanel.setVisible(false);
		expensesPanel.setVisible(false);
		balancePanel.setVisible(false);
		continueButton.setVisible(false);
	}

	@Override
	public void showProfit(int localProfit, int overseeProfit, int treasury) {
		localProfitSpan.setInnerText("" + localProfit);
		overseeProfitSpan.setInnerText("" + overseeProfit);
		treasurySpan.setInnerText("" + treasury);

		profitPanel.setVisible(true);
	}

	@Override
	public void showExpenses(int auxilia, int auxiliaExpenses, int army,
			int armyExpenses, int marines, int navyExpenses) {
		auxiliaSpan.setInnerText("" + auxilia);
		auxiliaExpensesSpan.setInnerText("" + auxiliaExpenses);
		armySpan.setInnerText("" + army);
		armyExpensesSpan.setInnerText("" + armyExpenses);
		marinesSpan.setInnerText("" + marines);
		navyExpensesSpan.setInnerText("" + navyExpenses);

		expensesPanel.setVisible(true);
	}

	@Override
	public void showBalance(int treasury) {
		balanceSpan.setInnerText("" + treasury);

		balancePanel.setVisible(true);
	}

	@Override
	public void showContinue() {
		continueButton.setVisible(true);
	}

	@UiHandler("continueButton")
	void onContinueButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onContinue();
		}
	}
}
